# 基于 ubuntu:22.04 构建的java maven 编译镜像

# FROM harbor.jcdiq.cn/library/gitlab/gitlab-runner:latest as gitlab-runner-java17
FROM ubuntu:22.04 as builder

LABEL maintainer="Libo <libo@jiachengnet.com>"
	
# Default to UTF-8 file.encoding
# ENV LANG en_US.UTF-8
ENV LANG zh_CN.UTF-8
# JDK17 下载解压

ENV JAVA_URL=https://download.oracle.com/java/17/latest \
	JAVA_HOME=/usr/java/jdk-17

##
# SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# sed -i 's/ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    chmod 777 /tmp && \
	apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates curl

RUN set -eux; \
	ARCH="$(uname -m)" && \
	# Java uses just x64 in the name of the tarball
    if [ "$ARCH" = "x86_64" ]; \
        then ARCH="x64"; \
    fi && \
    JAVA_PKG="$JAVA_URL"/jdk-17_linux-"${ARCH}"_bin.tar.gz ; \
	JAVA_SHA256="$(curl "$JAVA_PKG".sha256)" ; \
	curl --output /tmp/jdk.tgz "$JAVA_PKG" && \
	echo "$JAVA_SHA256" */tmp/jdk.tgz | sha256sum -c; \
	mkdir -p "$JAVA_HOME"; \
	tar --extract --file /tmp/jdk.tgz --directory "$JAVA_HOME" --strip-components 1
	
# maven3 下载解压
ENV MAVNE_VERSION=3.9.6 \
    MAVEN_URL=https://dlcdn.apache.org/maven/maven-3 \
	MAVEN_HOME=/opt/maven

RUN set -eux; \	
    MAVEN_PKG="$MAVEN_URL"/"$MAVNE_VERSION"/binaries/apache-maven-"$MAVNE_VERSION"-bin.tar.gz ; \
	MAVEN_SHA256="$(curl "$MAVEN_PKG".sha512)" ; \
	curl --output /tmp/maven.tgz "$MAVEN_PKG" && \
	echo "$MAVEN_SHA256" */tmp/maven.tgz | sha512sum -c; \
	mkdir -p "$MAVEN_HOME"; \
	tar --extract --file /tmp/maven.tgz --directory "$MAVEN_HOME" --strip-components 1

## Get a fresh version of OL8 for the final image	
FROM ubuntu:22.04

RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
	chmod 777 /tmp && \
    apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates locales git && \
	echo 'LANG="zh_CN.UTF-8"' > /etc/default/locale && \
	echo 'LANGUAGE="zh_CN:zh"' > /etc/default/locale && \
	locale-gen zh_CN.UTF-8

# Default to UTF-8 file.encoding
ENV LANG zh_CN.UTF-8
ENV	JAVA_HOME=/usr/java/jdk-17
ENV	MAVEN_HOME=/opt/maven
ENV	PATH $JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

# Copy the uncompressed Java Runtime from the builder image
COPY --from=builder $JAVA_HOME $JAVA_HOME
COPY --from=builder $MAVEN_HOME $MAVEN_HOME
ADD settings.xml ${MAVEN_HOME}/conf/

RUN	ln -sfT "$JAVA_HOME" /usr/java/default; \
	ln -sfT "$JAVA_HOME" /usr/java/latest; \
	for bin in "$JAVA_HOME/bin/"*; do \
		base="$(basename "$bin")"; \
		[ ! -e "/usr/bin/$base" ]; \
		update-alternatives --install "/usr/bin/$base" "$base" "$bin" 20000; \
	done; \
	mkdir -p /var/repository;